<?php
declare (strict_types=1);

namespace app\common\service;

use app\common\logic\category\Category;
use app\common\logic\Common as CommonLogic;
use app\common\queue\promotion\Tuan;
use app\user\logic\user\Account;
use think\annotation\Inject;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Request;
use think\Response;

/**
 * 公共接口类
 */
class Common extends BaseService
{
    /**
     * @Inject()
     * @var Category
     */
    protected $cate;

    /**
     * 分类树
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2020-06-12
     */
    public function getCategoryTree(): Response
    {
        $type   = Request::has('type', 'get', true) ? Request::get('type/d') : 0;
        $mhm_id = (Request::has('mhm_id', 'get', true) && Request::get('mhm_id/d')) ? Request::get('mhm_id/d') : request()->mhm_id;
        $cate   = $this->cate->getSelectTree(0, $type, [['mhm_id', '=', $mhm_id]]);

        return static::parseToData($cate, 1, '获取成功');
    }

    /**
     * 一级分类
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/12/05
     *
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getTopCategory(): Response
    {
        $type   = input('?get.type') ? input('get.type/d') : 0;
        $mhm_id = (input('?get.mhm_id') && $mhm = input('get.mhm_id/d')) ? $mhm : request()->mhm_id;
        $cate   = $this->cate->getTopCategory($type, [['mhm_id', '=', $mhm_id]]);

        return static::parseToData($cate, 1, '获取成功');
    }

    // 苹果ios设备登录
    public function ioslogin(): Response
    {
        $param = Request::post();
        $re    = app(Account::class)->ioslogin($param);
        return static::parseToData($re, 1, '登录成功');
    }

    // 苹果ios设备登录后绑定
    public function iosloginBind(): Response
    {
        $param = Request::post();
        $re    = app(Account::class)->iosloginBind($param);
        return static::parseToData($re, 1, '绑定成功');
    }

    public function shareInfo(): Response
    {
        $param = Request::get();
        $re    = app(CommonLogic::class)->shareInfo($param);
        return static::parseToData($re, 1, '获取成功');
    }

    /**
     * shareSignature
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 23/09/08
     *
     * @return Response
     */
    public function shareSignature()
    {
        $url = Request::get('url/s');
        $re    = app(CommonLogic::class)->shareSignature($url);
        return static::parseToData($re, 1, '获取成功');
    }

    // public function queueTuan()
    // {
    //     app(Tuan::class)->run();
    //     return static::parseToData([], 1, '获取成功');
    // }
}
